﻿using System;
using System.Collections.Generic;
using Lottery.App.Model;
using Lottery.App.Core;

namespace Lottery.App.DAL
{
    public class D1T2Dao : ResultDao<D1T2Info>
    {
        protected override IList<D1T2Info> GetItems(RecordInfo record)
        {
            string[] items = record.ASC_Result.Split(' ');

            string hql = "from D1T2Info where (1=2)";

            for (int i = 0; i < items.Length; i++)
            {
                for (int j = 0; j < items.Length; j++)
                {
                    if (i == j) continue;

                    hql += String.Format(" or (DM = '{0}' and TM like '%{1}%')", items[i], items[j]);
                }
            }

            return Session.CreateQuery(hql).List<D1T2Info>();
        }

        internal override IList<RecordInfo> List(string value)
        {
            string[] values = value.Split(' ');

            string hql = "from RecordInfo where ASC_Result like ? and (ASC_Result like ? or ASC_Result like ?)";

            return Session.CreateQuery(hql)
                .SetString(0, MatchMode.Anywhere(values[0]))
                .SetString(1, MatchMode.Anywhere(values[1]))
                .SetString(2, MatchMode.Anywhere(values[2]))
                .List<RecordInfo>();
        }

        protected override int GetLastID(string value)
        {
            string[] values = value.Split(' ');

            //string hql = "select Max(r.id) from RecordInfo r where ASC_Result like ? and (ASC_Result like ? or ASC_Result like ?)";

            string hql = "select Max(r.LastID) from N2Info r where Value like ? and (Value like ? or Value like ?)";

            return Session.CreateQuery(hql)
                .SetString(0, MatchMode.Anywhere(values[0]))
                .SetString(1, MatchMode.Anywhere(values[1]))
                .SetString(2, MatchMode.Anywhere(values[2]))
                .UniqueResult<int>();
        }

        public override IList<int> GetIds(string value)
        {
            string[] values = value.Split(' ');

            string hql = "select r.id from RecordInfo r where ASC_Result like ? and (ASC_Result like ? or ASC_Result like ?)";

            return Session.CreateQuery(hql)
                .SetString(0, MatchMode.Anywhere(values[0]))
                .SetString(1, MatchMode.Anywhere(values[1]))
                .SetString(2, MatchMode.Anywhere(values[2]))
                .List<int>();
        }

        public override IList<int> GetIds(string value, int start)
        {
            string[] values = value.Split(' ');

            string hql = "select r.id from RecordInfo r where r.id > ? and ASC_Result like ? and (ASC_Result like ? or ASC_Result like ?)";

            return Session.CreateQuery(hql)
                .SetInt32(0, start)
                .SetString(1, MatchMode.Anywhere(values[0]))
                .SetString(2, MatchMode.Anywhere(values[1]))
                .SetString(3, MatchMode.Anywhere(values[2]))
                .List<int>();
        }
    }
}
